package model;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.DiscriminatorColumn;
import javax.persistence.DiscriminatorType;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Transient;

/**
 * @author soreas + morarg Entity implementation class for Entity: Folder
 * 
 */
@Entity
@Table(name = "folder")
@Inheritance(strategy = InheritanceType.JOINED)
@DiscriminatorColumn(name = "DISC", discriminatorType = DiscriminatorType.STRING, length = 20)
@DiscriminatorValue("Folder")
public class Folder implements Serializable {

	@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "seq")
	@Id
	@SequenceGenerator(name = "seq", initialValue = 1, allocationSize = 100)
	@Column(name = "ID")
	private Integer id;

	@Column(nullable = false)
	private String name;

	@Column(nullable = false)
	private Integer version;

	private Folder folder;
	private static final long serialVersionUID = 1L;

	@Transient
	private Folder repository;

	@Transient
	private String path;

	public Folder() {
		super();
	}

	public Integer getId() {
		return this.id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getName() {
		return this.name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Integer getVersion() {
		return this.version;
	}

	public void setVersion(Integer version) {
		this.version = version;
	}

	public Folder getFolder() {
		return folder;
	}

	public void setFolder(Folder folder) {
		this.folder = folder;
	}

	/**
	 * Getter for this folder's repository. The returned object is of
	 * <b>Folder</b> type!
	 * 
	 * @return The repository to which this folder belongs to.
	 */
	@Transient
	public Folder getRepository() {
		if (repository == null) {
			if (this.id == this.folder.id) {
				repository = this;
			} else {
				repository = folder.getRepository();
			}
		}

		return repository;
	}

	@Override
	public String toString() {
		return "Folder [id=" + id + ", name=" + name + ", version=" + version
				+ ", folder=" + folder + "]";
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Folder other = (Folder) obj;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		return true;
	}

	@Transient
	public String getPath() {
		if (path == null) {
			path = addPath(this);
		}
		return path;
	}

	/**
	 * returneaza toata calea pana la repository a unui folder
	 * 
	 * @author fodorl
	 * @return String
	 */

	public String addPath(Folder folder) {
		if (folder.getId() == folder.getFolder().getId()) {
			return folder.getName();
		} else {
			return (addPath(folder.getFolder()) + " / " + folder.getName());
		}
	}

	public void setPath(String path) {
		this.path = path;
	}

}
